package com.malljelly.study.controller;

import com.alibaba.fastjson.JSONObject;
import com.malljelly.study.dao.po.PaymentInfo;
import com.malljelly.study.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 骑着蜗牛去旅行 on 2018/8/29.
 */
@Api("支付宝接口文档")
@RestController  //@Controller+@ResponseBody
@RequestMapping("soa/aliPayService")
@Slf4j
public class AliPayController {

    @Autowired
    private PayService payService;


    /***
     * {
     "created": "2018-08-31",
     "orderId": "123456789",
     "price": 50000,
     "source": "member",
     "state": 0,
     "typeId": 1,
     "updated": "2018-08-31",
     "userId": "123456"
     }
     * @param paymentInfo
     * @return
     */
    @ApiOperation(value = "创建支付令牌token,存入redis中设置过期时间")
    @PostMapping("/getPayToken")
    JSONObject getPayToken(@RequestBody PaymentInfo paymentInfo){
        return  payService.getPayToken(paymentInfo);
    }


    @ApiOperation(value = "使用支付令牌获取支付信息,访问支付宝网页")
    @GetMapping("/payInfo/{token}")
    void payInfo(@PathVariable("token") String token,HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        JSONObject jsonObject = payService.payInfo(token);
        Object code = jsonObject.get("code");
        if(-1==  Integer.parseInt(code.toString()) ){
            return ;
        }
        String payInfoHtml =jsonObject.get("msg").toString();
        out.println(payInfoHtml);
        out.close();
    }

}
